const util = require('../../utils/util.js')
const app = getApp();

Page({
    data: {
        topCategories: [],
        subCategories: [],
        highlight:['highlight','',''],
        banner: ''
    },
    onLoad: function(){
      this.initCategory();
    },
    initCategory:function(){
      let that = this;
      let param = {};
      util.commonRequest('index/Category/index','post',param,function(res){
        if (res.data.code == 200){
          that.setData({
            topCategories:res.data.data
          })
          that.initSubcategory();
          // that.getBanner(that.data.topCategories[0]['image']);
        }
      })
    },
  initSubcategory:function(){
    let data = this.data.topCategories;
    this.subCategory(data[0]['id']);
  },
  subCategory:function(fid){
    // debugger
      let that = this;
      let param = {
        fid:fid
      };
      util.commonRequest('index/Category/subCategory','post',param,function(res){
         if (res.data.code == 200){
           that.setData({
             subCategories:res.data.data
           })
         }
      })
    },
    setImageWidth: function () {
        var screenWidth = getApp().screenWidth;
        var imageWidth = (screenWidth - 130) / 3 - 5;
        this.setData({
            imageWidth: imageWidth
        });
    },
    setSideHeight: function () {
        this.setData({
            sidebarHeight: getApp().screenHeight
        });
    },
    tapTopCategory: function(e){
        let logo = e.currentTarget.dataset.logo;
        let categoryId = e.currentTarget.dataset.id;
        this.subCategory(categoryId);

        // this.getCategory(parent);
        // 设定高亮状态
        var index = parseInt(e.currentTarget.dataset.index);
        this.setHighlight(index);
        // get banner local
        this.getBanner(logo);

    },
    getCategory: function(parent){
        var that = this;
        var query = new AV.Query('Category');
        // 查询顶级分类，设定查询条件parent为null
        query.equalTo('parent',parent);
        query.ascending('index');
        query.find().then(function (categories) {
            if (parent){
                that.setData({
                    subCategories: categories
                });
            }else{
                that.setData({
                    topCategories: categories
                });
            }
        }).catch(function(error) {
        });
    },
    setHighlight: function(index){
        var highlight = [];
        for (var i = 0; i < this.data.topCategories; i++) {
            highlight[i] = '';
        }
        highlight[index] = 'highlight';
        this.setData({
            highlight: highlight
        });
    },
    avatarTap: function(e){
        // 拿到objectId，作为访问子类的参数
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: "../../../../goods/list/list?categoryId="+id
        });
    },
    getBanner: function (image) {
      var that = this;
      that.setData({
        banner: image
      });  
    },
    showGoods: function () {
        wx.navigateTo({
            url: '../goods/detail/detail?objectId=5816e3b22e958a0054a1d711'
        });
    }
})